package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Likes;
import cn.wolfcode.trip.base.service.ILikesService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/likes")
public class LikesController {

    @Autowired
    private ILikesService likesService;

    @GetMapping
    public Object getLikes(Long id){
        List<Likes> likes = likesService.selectLikesByUserId(id);
        return likes;
    }
    @PutMapping("/changeState/{travelId}")
    public Object changeState(@PathVariable Long travelId){
        likesService.changeState(travelId);
        return new JSONResult();
    }

    //在登录状态下,根据用户id和文章id查询是否有当前点赞的信息
    /**
     * 获取当前点赞信息
     * 资源 /likes
     * 动作 GET
     * 参数/{userId}/{contentId}
     */
    @GetMapping("/{userId}/{contentId}")
    public  Object getState(@PathVariable Long userId, @PathVariable Long contentId ){

        Likes likes = likesService.selectLikesByUserId(userId, contentId);

       /* if(likes.isState()){
            //当前已经点赞
        }*/
        if (likes == null) {
            //用户没有点过赞
            return false;
        }
        return true;
    }


    /**保存和删除当前点赞信息
     * 资源 /likes
     * 动作 POST
     * 参数/{userId}/{contentId}
     *
     */
    @PostMapping("/{userId}/{contentId}")
    public  void saveState(@PathVariable Long userId, @PathVariable Long contentId ){

        //返回点赞数量
        likesService.save(userId,contentId);
    }

}
